<template>
    <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon >
        <el-form-item label="选择物料" prop="warehouse_id">
            <component :is="warehouseSelect" :data=ruleForm.warehouse_id @onWarehouseSelect="onWarehouseSelect" />
        </el-form-item>
        <el-form-item label="批次号" prop="numbers">
            <el-input v-model="ruleForm.numbers" />
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
            <el-input-number v-model="ruleForm.quantity" :min="1" />
        </el-form-item>
        <el-form-item label="出库日期" prop="out_date">
            <el-date-picker v-model="ruleForm.out_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="出库单" prop="files">
            <el-input v-model="ruleForm.files" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
            <el-input v-model="ruleForm.remarks" type="textarea" :autosize="{ minRows: 4 }" />
        </el-form-item>
    </el-form>
    <el-divider />
    <div class="text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
    </div>
</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus';
import http from '../../../../util/request';
import warehouseSelect from "./warehouseSelect.vue";
const loading = ref<boolean>(false)

interface RuleForm {
    id: number
    files:string
    warehouse_id:number
    quantity: number
    numbers: string
    out_date: string
    remarks: string
}

const formSize = ref<ComponentSize>('large')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id: 0,
    files:'',
    warehouse_id:0,
    quantity: 0,
    numbers: '/',
    out_date: '',
    remarks: '/',
})

const onGetData = () => {
    if(!ruleForm.value.id){
        return;
    }
    http.get('/warehouse/'+ruleForm.value.id).then((response:any)=>{
        if(response.data.data){
            ruleForm.value = response.data.data
        }
    })
}

const props = defineProps<{
    data:any
}>()

watch(props,()=>{
    ruleForm.value.id = props.data
    ruleForm.value.numbers = '/'
    ruleForm.value.quantity = 0
    ruleForm.value.out_date = ''
    ruleForm.value.remarks = '/'
    onGetData()
},{ immediate: true })

const rules = reactive<FormRules<RuleForm>>({
    numbers: [
        { required: true, message: '请输入批次号', trigger: 'blur' },
        { min: 2, max: 30, message: '范围在2-30字之间', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            http.put('/warehouse/outs',ruleForm.value).then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);
                    return
                }
                onSave()
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

const onWarehouseSelect = (data:number) => {
    ruleForm.value.warehouse_id = data
}

const onSave = () => {
    emit('onSave')
}

const emit = defineEmits(['onSave'])
</script>
<style scoped>
</style>
